//     https://ac.nowcoder.com/acm/problem/16693




#include<iostream>
using namespace std;

int v, n;
int a[40];
int dp[20010];

int main()
{
    cin >> v >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    dp[0] = 0;

    for (int i = 1; i <= n; i++)
        for (int j = v; j >= a[i]; j--)
            dp[j] = max(dp[j], dp[j - a[i]] + a[i]);

    for (int j = v; j >= 0; j--)
    {
        if (dp[j] > 0)
        {
            cout << v - dp[j] << endl;
            break;
        }
    }
    return 0;
}